rm(list = ls())
library(ggplot2)    
library(tidyverse) 
library(readxl)
library(haven)
library(DirectEffects)
library(sensemakr)


# Load Data ----------------------------------------------------------
df <- read_dta("data/clean_survey.dta")

# Table 4 ----------------------------------------------------------

# Column 1 
m <- military_legitimacy ~ 
  # first stage 
  treated + factor(education) + age +  vote + pop_density + road_density + res_density + 
  IS_any_harm + factor(income_pre_IS) |
  # intermediate confounders 
  factor(identity) + factor(sharia) + factor(prayer) + daesh_govern + factor(daesh_corrupt) + factor(daesh_taxes) + factor(corrupt_current) |
  # de-mediated variables 
  house_damage
sg <- sequential_g(m, data = df)

summary(sg)
mean(sg$model$military_legitimacy)
nrow(sg$model)

# Column 2
m <- military_legitimacy ~ 
  # first stage 
  treated + factor(education) + age +  vote + pop_density + road_density + res_density + 
  IS_any_harm + factor(income_pre_IS) |
  # intermediate confounders 
  factor(identity) + factor(sharia) + factor(prayer) + daesh_govern + factor(daesh_corrupt) + factor(daesh_taxes)  + factor(corrupt_current) |
  # de-mediated variables 
  death_or_injury
sg <- sequential_g(m, data = df)

summary(sg)
mean(sg$model$military_legitimacy)
nrow(sg$model)

# Column 3
m <- military_legitimacy ~ 
  # first stage 
  treated + factor(education) + age +  vote + pop_density + road_density + res_density + 
  IS_any_harm + factor(income_pre_IS) |
  # intermediate confounders 
  factor(identity) + factor(sharia) + factor(prayer) + daesh_govern + factor(daesh_corrupt) + factor(daesh_taxes)  + factor(corrupt_current) |
  # de-mediated variables 
  damage_in_10m
sg <- sequential_g(m, data = df)

summary(sg)
mean(sg$model$military_legitimacy)
nrow(sg$model)


# Column 4
m <- military_legitimacy ~ 
  # first stage 
  treated + factor(education) + age +  vote + pop_density + road_density + res_density + 
  IS_any_harm + factor(income_pre_IS) |
  # intermediate confounders 
  factor(identity) + factor(sharia) + factor(prayer) + daesh_govern + factor(daesh_corrupt) + factor(daesh_taxes) + factor(corrupt_current) |
  # de-mediated variables 
  house_damage+ death_or_injury+ damage_in_10m
sg <- sequential_g(m, data = df)

summary(sg)
mean(sg$model$military_legitimacy)
nrow(sg$model)

# Table A-5 ----------------------------------------------------------
m <- lm(military_legitimacy ~ treated + gender + age + factor(income_pre_IS) + 
              factor(identity) + factor(education) + vote + gov_grievance_police + 
              gov_grievance_arrested + gov_grievance_sunni + gov_grievance_protest + 
              IS_any_harm + IS_electric + IS_water + IS_zakat + pop_density + 
              road_density + res_density, data = df)

sensitivity <- sensemakr(model = m, 
                         treatment = "treated",
                         benchmark_covariates = "gov_grievance_sunni", 
                         kd = c(1,5,10))
ovb_minimal_reporting(sensitivity)

# Table A-6 ----------------------------------------------------------
summary(sensitivity)

# Table A-14 ----------------------------------------------------------

# Column 1
m <- military_legitimacy ~ 
  # first stage 
  treated + factor(education) + age +  vote + pop_density + road_density + res_density + 
  IS_any_harm + factor(income_pre_IS) |
  # intermediate confounders 
  factor(identity) + factor(sharia) + factor(prayer) + daesh_govern + factor(daesh_corrupt) + factor(daesh_taxes)  + factor(corrupt_current) |
  # de-mediated variables 
  house_damage+ death_or_injury+ damage_in_10m
sg <- sequential_g(m, data = df)

summary(sg)
mean(sg$model$military_legitimacy)
nrow(sg$model)

# Column 2
m <- military_legitimacy ~ 
  # first stage 
  treated + factor(education) + age +  vote + pop_density + road_density + res_density + 
  IS_any_harm + factor(income_pre_IS) |
  # intermediate confounders 
  factor(identity) + factor(sharia) + factor(prayer) + daesh_govern + factor(daesh_corrupt) + factor(daesh_taxes)  + factor(corrupt_current) |
  # de-mediated variables 
  house_damage+ death_or_injury+ damage_in_50m
sg <- sequential_g(m, data = df)

summary(sg)
mean(sg$model$military_legitimacy)
nrow(sg$model)

# Column 3
m <- military_legitimacy ~ 
  # first stage 
  treated + factor(education) + age +  vote + pop_density + road_density + res_density + 
  IS_any_harm + factor(income_pre_IS) |
  # intermediate confounders 
  factor(identity) + factor(sharia) + factor(prayer) + daesh_govern + factor(daesh_corrupt) + factor(daesh_taxes)  + factor(corrupt_current) |
  # de-mediated variables 
  house_damage+ death_or_injury+ damage_in_100m
sg <- sequential_g(m, data = df)

summary(sg)
mean(sg$model$military_legitimacy)
nrow(sg$model)

# Column 4
m <- military_legitimacy ~ 
  # first stage 
  treated + factor(education) + age +  vote + pop_density + road_density + res_density + 
  IS_any_harm + factor(income_pre_IS) |
  # intermediate confounders 
  factor(identity) + factor(sharia) + factor(prayer) + daesh_govern + factor(daesh_corrupt) + factor(daesh_taxes)  + factor(corrupt_current) |
  # de-mediated variables 
  house_damage+ death_or_injury+ damage_in_500m
sg <- sequential_g(m, data = df)

summary(sg)
mean(sg$model$military_legitimacy)
nrow(sg$model)

